﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.Module.BillManagement
{
    public partial class BillsLedger_Bu : BasePage
    {
        private YX_BILLSLEDGER_BU_BLL blbubll = new YX_BILLSLEDGER_BU_BLL();
        protected string filePath = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindBank();

                FORM_BEGIN_DATE.Value = "";

                FORM_OVER_DATE.Value = "";

                ViewState["SortOrder"] = "T1.N_ID";
                ViewState["OrderDire"] = " desc ";

                BindData(true);
            }
        }

        private void Bind()
        {
            string whereStr = "";
            //出票账号
            if (txtDRAWERACCOUNT.Value.Trim() != "")
            {
                whereStr += " AND T1.C_DRAWERACCOUNT LIKE '%" + txtDRAWERACCOUNT.Value.Trim() + "%'";
            }
            //票据种类
            if (FORM_Type.SelectedValue != "-1")
            {
                whereStr += " AND T1.N_TYPE =" + Convert.ToDecimal(FORM_Type.SelectedValue.ToString());
            }
            //所属公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " AND T2.USER_COMPANY=" + FORM_COMPANY_ID.Value;
            }
            else
            {
                whereStr += @" AND T2.USER_COMPANY in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            //出票银行
            if (txtDRAWERBank.Value != "")
            {
                whereStr += " AND T1.C_DRAWERBANKNAME LIKE '%" + txtDRAWERBank.Value + "%'";
            }

            //票据日期
            if (FORM_BEGIN_DATE.Value.Trim() != "" && FORM_OVER_DATE.Value.Trim() != "")
            {
                whereStr += @" and T1.D_STARTDATE between
       TO_DATE('" + FORM_BEGIN_DATE.Value + @"', 'YYYY-MM-DD') and
       TO_DATE('" + FORM_OVER_DATE.Value + @"', 'YYYY-MM-DD') ";
            }

            ViewState["Where"] = whereStr;

            BindData(true);
        }

        #region 查增改删

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow1.Rows.Count; i++)
            {
                if (((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow1.Rows[i].FindControl("chkSelectId")).ToolTip;
                        string billid = grvShow1.Rows[i].Cells[0].Text.ToString();
                        MessageBox.ResponseScript(this, "FORM_EDIT(" + billid + "," + pid + ");");
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            var keys = DELETE_CHECK_IDS.Value.Split(',');
            var dsql = "";
            var iftrue = false;
            for (int i = 0; i < keys.Length; i++)
            {
                dsql = "DELETE FROM YX_BILLSLEDGER_BU WHERE  N_ID =" + keys[i];
                if (blbubll.NoQuery(dsql) > 0)
                {
                    iftrue = true;
                }
                else
                {
                    iftrue = false;
                    MessageBox.Show(Page, "第" + i + 1 + "条数据删除失败！");
                }
            }
            if (iftrue)
            {
                MessageBox.Show(this, "删除成功。");
                BindData(false);
            }
            else
            {
                MessageBox.Show(this, "删除失败。");
            }
            BindData(false);
        }

        #endregion 查增改删

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //点击排序
            string sPage = e.SortExpression;

            if (sPage == "")
            {
                return;
            }
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
                ViewState["OrderDire"] = "ASC";
            }

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "AND T2.USER_COMPANY=" + FORM_COMPANY_ID.Value;
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = blbubll.GetRecordCount(Where, lognUser.USER_NAME);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = blbubll.GetListByPage(Where, sort, PageIng.CurrentPage, PageIng.RowsPerPage);
            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                grvShow1.DataSource = null;
                grvShow1.DataBind();
                PageIng.Visible = false;
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaoChu_Click(object sender, EventArgs e)
        {
            var dt = (DataTable)ViewState["database"];

            #region 导出一

            dt.Columns["R"].ColumnName = "序号";
            dt.Columns["N_TYPE"].ColumnName = "票据类型";
            dt.Columns["C_BILLCODE"].ColumnName = "票据号码";
            dt.Columns["C_DRAWERCMYNAME"].ColumnName = "出票人公司名称(出票人)";
            dt.Columns["C_DRAWERBANKNAME"].ColumnName = "出票人银行名称";
            dt.Columns["C_DRAWERACCOUNT"].ColumnName = "出票人账户";
            dt.Columns["C_DRAWEENAME"].ColumnName = "付款人";
            dt.Columns["C_PAYEECMYNAME"].ColumnName = "收款人公司名称(收款人)";
            dt.Columns["C_PAYEEBANKNAME"].ColumnName = "收款人银行名称";
            dt.Columns["C_PAYEEACCOUNT"].ColumnName = "收款人账户";
            dt.Columns["C_PAYEENUMBER"].ColumnName = "收款人联系电话";
            dt.Columns["N_INVOICEVALUE"].ColumnName = "出票金额";
            dt.Columns["D_STARTDATE"].ColumnName = "出票日期";
            dt.Columns["D_ENDDATE"].ColumnName = "票据到期日";
            dt.Columns["N_TERMOFBILL"].ColumnName = "票据期限（月）";
            dt.Columns["D_ACCEPTANCETIME"].ColumnName = "承兑时间";
            dt.Columns["D_COLLECTTICKETSTIME"].ColumnName = "收票时间";
            dt.Columns["C_PROJECTNAME"].ColumnName = "项目名称（用途）";
            dt.Columns["C_PROJECTCODE"].ColumnName = "交易合同号（用途）";
            dt.Columns["C_REMARK"].ColumnName = "备注";

            //dt.Columns["USERNAME"].ColumnName = "录入员";
            //dt.Columns["N_IFOFFER"].ColumnName = "能否背书转让";
            //dt.Columns["N_PAYTYPE"].ColumnName = "支付类型";
            //dt.Columns["N_STATE"].ColumnName = "票据状态";
            //dt.Columns["N_ENTRYSTAFF"].ColumnName = "录入员";
            dt.Columns.Remove("N_ID");
            dt.Columns.Remove("N_DRAWERCMYID");
            dt.Columns.Remove("N_PAYEECMYID");
            dt.Columns.Remove("N_SUM");
            dt.Columns.Remove("CMYID");

            dt.Columns.Remove("N_IFOFFER");
            dt.Columns.Remove("N_PAYTYPE");
            dt.Columns.Remove("N_ENTRYSTAFF");
            dt.Columns.Remove("N_STATE");
            dt.Columns.Remove("USERNAME");

            #endregion 导出一

            toExecl1(dt, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "票据信息明细");
        }
    }
}